/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 *

 *
 */
package Modulo4;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import Modelo.Asistencia;
import Modelo.DAL.Asistencia_DAO;

import Acceso.Security;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author Jose Leonardo
 *
 */
public class registrarAsistenciaAction extends org.apache.struts.action.Action {

    // forward name="success" path="" *
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     **/
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String[] a = {"Administrador", "Llevar Control de Asistencias"};
        if (!Security.validateSession(request, a)) {
            return mapping.findForward("errorPriv");
        }
        registrarAsistenciaForm formBean = (registrarAsistenciaForm) form;

        String ci = formBean.getCi();
        String idGuardia = formBean.getIdGuardia();
        String espacio = formBean.getNombreEspacio();
        String sede = formBean.getNombreSede();
        String horaLleg = formBean.getHoraLlegada();
        String horaSal = formBean.getHoraSalida();
        String observ = formBean.getObservaciones();
        String condicion = formBean.getCondicion();
        String status = formBean.getStatus();

        boolean error = false;
        ActionErrors errors = new ActionErrors();

        if (ci == null || ci.equals("")) {
            errors.add("cedula.requerida", new ActionMessage("errors.cedula.requerida"));
            saveErrors(request, errors);
            error = true;
        }
        if (condicion == null || condicion.equals("")) {
            errors.add("condicion.requerida", new ActionMessage("errors.condicion.requerida"));
            saveErrors(request, errors);
            error = true;
        } else if (condicion.equals("presente")) {
            if (horaLleg == null || horaLleg.equals("")) {
                errors.add("hora.requerida", new ActionMessage("errors.hora.requerida"));
                saveErrors(request, errors);
                error = true;
            } else if (horaLleg.equals(horaSal)) {
                errors.add("horas.iguales", new ActionMessage("errors.horas.iguales"));
                saveErrors(request, errors);
                error = true;
            }
        }
        if (error) {
            System.out.println("No se puede registrar la asistencia");
            return mapping.findForward(FAILURE);
        }

        Asistencia asist = new Asistencia(ci, Integer.parseInt(idGuardia), horaLleg, horaSal, sede, condicion, status, observ);
        asist.setNombreEspacio(espacio);
        Asistencia_DAO asistD = new Asistencia_DAO();

        if (!asistD.insertarAsistencia(asist)) {
            return mapping.findForward(FAILURE);
        }

        formBean.reset();
        return mapping.findForward(SUCCESS);
    }
}
